package com.smartcommunity.controller.people;

import com.smartcommunity.bean.entity.User;
import com.smartcommunity.bean.model.Pager;
import com.smartcommunity.elasticsearch.face.FaceEsApi;
import com.smartcommunity.elasticsearch.face.req.FaceEsReq;
import com.smartcommunity.bean.model.Result;
import com.smartcommunity.controller.BaseController;
import com.smartcommunity.entity.vid.Face;
import com.smartcommunity.entity.vid.FaceSearchModel;
import com.smartcommunity.log.interceptor.Log;
import com.smartcommunity.service.CommonService;
import com.smartcommunity.utils.ArrayUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @ClassName: FaceController
 * @Description: 人脸服务
 * @author: lilun
 * @Date: 2019/12/30 17:45
 * @Company:
 */
@RestController
@RequestMapping(value = "/face")
@Api(value = "小区服务-人脸", tags = "人脸通行记录")
@Slf4j
public class FaceController extends BaseController {

    @Autowired
    private FaceEsApi faceEsApi;

    @Autowired
    private CommonService commonService;

    @Deprecated
    @PostMapping(value = "/searchCaptureFace")
    @Log(info = "查询人脸最近抓拍信息",module = "人脸通行记录",function = "查询")
    @ApiOperation(value = "查询人脸最近抓拍信息", notes = "查询人脸最近抓拍信息")
    public Result searchRecentlyCaptureFace(@Valid @RequestBody FaceEsReq faceEsReq) {
        return Result.ok(faceEsApi.searchRecentlyCaptureFace(faceEsReq));
    }

    @PostMapping(value = "/searchFaceList")
    @Log(info = "查询人脸通行记录",module = "人脸通行记录",function = "查询")
    @ApiOperation(value = "查询人脸通行记录", notes = "查询人脸通行记录")
    public Result searchFaceList(@Valid @RequestBody FaceSearchModel faceSearchModel) {

        if (StringUtils.isBlank(faceSearchModel.getVillageCode())) {
            User user = getCurrentUser();
            String villageCode = commonService.getVillageCodeByUser(user);
            if (StringUtils.isNotEmpty(faceSearchModel.getParentId())) {
                String orgIds = commonService.getAllChildOrgId(faceSearchModel.getParentId());
                Map<String, Object> map = new HashedMap();
                map.put("orgId", orgIds);
                map.put("villageCode", villageCode);
                List<String> villageList = commonService.listVillageCodeByOrgId(map);
                //此行政区划节点下没有小区，则返回
                if (CollectionUtils.isEmpty(villageList)){
                    return Result.ok();
                }
                villageCode = ArrayUtils.toString(villageList);
            }
            faceSearchModel.setVillageCode(villageCode.replaceAll("'", ""));
        }

        return Result.ok(faceEsApi.searchFaceList(faceSearchModel));
    }

    @GetMapping(value = "/searchFaceById")
    @Log(info = "查询人脸通行记录",module = "人脸通行记录",function = "查询")
    @ApiOperation(value = "查询人脸通行记录", notes = "查询人脸通行记录")
    public Result searchFaceById(@Valid @RequestParam String recordId) {
        FaceSearchModel faceSearchModel = new FaceSearchModel();
        faceSearchModel.setPage(1);
        faceSearchModel.setPageSize(1);
        faceSearchModel.setAccessLogId(recordId);
        Pager<Face> page = faceEsApi.searchFaceList(faceSearchModel);
        return Result.ok(CollectionUtils.isNotEmpty(page.getList())?page.getList().get(0):null);
    }

    @PostMapping(value = "/searchFeatureList")
    @Log(info = "查询人脸通行特征记录",module = "人脸通行记录",function = "查询")
    @ApiOperation(value = "查询人脸通行特征记录")
    public Result searchFeatureList(@RequestBody FaceSearchModel faceSearchModel) {
        if (faceSearchModel.getTop() < 1 || faceSearchModel.getTop() > 9999) {
            return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Top 值必须在[1,9999]区间");
        }
        return Result.ok(faceEsApi.searchFeatureList(faceSearchModel));
    }
}
